import { useEffect } from 'react'

export function useOutsideClick(el: any, callback: () => void): void {
  useEffect(() => {
    function handleClickOutside(event: MouseEvent) {
      const target = event.target
      if (el && !el.contains(target)) {
        callback()
      }
    }
    document.addEventListener('click', handleClickOutside)
    return () => {
      document.removeEventListener('click', handleClickOutside)
    }
  }, [el, callback])
}
